/*
 * 文件名： SpdbMicroPayManager.java
 * 
 * 工程名称: bis-manager
 *
 * Gopay
 *
 * 创建日期： 2017年11月13日
 *
 * Copyright(C) 2017, by www.gopay.com.cn Inc.All rights reserved.
 *
 * 原始作者: 宣广海
 *
 */
package com.gopay.bis.manager.bank.micro;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.BeanUtils;

import com.gopay.common.constants.bank.SpdbConstants;
import com.gopay.common.domain.bank.BaseMessage;
import com.gopay.common.domain.bank.spdb.agpay.MicroPayBody;
import com.gopay.common.domain.bank.spdb.agpay.PayGoodsList;
import com.gopay.common.domain.bank.spdb.agpay.PayNotify;
import com.gopay.common.domain.bank.spdb.agpay.QueryOrderBody;
import com.gopay.common.domain.bank.spdb.agpay.RefundBody;
import com.gopay.common.domain.bank.spdb.agpay.ReverseBody;
import com.gopay.common.exception.GopayException;
import com.gopay.common.util.FastJsonUtil;
import com.gopay.common.util.TransUtil;
import com.gopay.remote.bank.businesses.MicroPayService;
import com.gopay.remote.bank.businesses.ScanPayService;

/**
 * 聚合支付-条码支付浦发银行接口
 *
 * @author 宣广海
 *
 * @version
 *
 * @since 2017年11月13日
 */
public class SpdbMicroPayManager {

	@Resource(name = "agPaySpdbPayConfig")
	private Map<String, String> agPaySpdbPayConfig;
	@Resource(name = "agPaySpdbServiceClient")
	private ScanPayService agPaySpdbServiceClient;
	@Resource(name = "agPaySpdbExtServiceClient")
	private MicroPayService agPaySpdbExtServiceClient;

	/**
	 * 
	 * 功能描述：条码支付报文构建
	 *
	 * @param msg
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2017年11月15日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object payment(BaseMessage baseMsg) throws GopayException {

		MicroPayBody message = new MicroPayBody();
		BeanUtils.copyProperties(baseMsg, message);
		
		PayGoodsList goodsMessage = new PayGoodsList();
		baseMsg.setBankOrderId(modifyOrderId(baseMsg.getOrderId()));
		message.setVersion(agPaySpdbPayConfig.get("SPDB.AGPAY.VERSION"));
		message.setSignType(agPaySpdbPayConfig.get("SPDB.AGPAY.SIGNTYPE"));
		message.setCharset(agPaySpdbPayConfig.get("SPDB.AGPAY.CHARSET"));
		message.setTxndir("Q");// 交易方向，Q为请求，A为应答
		message.setBusicd(SpdbConstants.PURC);
		message.setInscd(agPaySpdbPayConfig.get("SPDB.AGPAY.INSCD"));// 机构号
		message.setMchntid(baseMsg.getMerID());
		message.setTerminalid(agPaySpdbPayConfig.get("SPDB.AGPAY.TERMINALID"));// 终端号
		message.setOrderNum(baseMsg.getBankOrderId());
		message.setTxamt(TransUtil.amountConvert2Fen(String.valueOf(baseMsg.getAmount()), 12, "0"));
		message.setCurrency("CNY");

		goodsMessage.setGoodsName(message.getGoodsBody());
		goodsMessage.setPrice(TransUtil.amountConvert2Fen(String.valueOf(baseMsg.getAmount())));// 精确到分不需要补位
		goodsMessage.setGoodsNum("1");// 数量
		goodsMessage.setGoodsId(message.getGoodsBody());// 商品条码

		List<PayGoodsList> goodsList = new ArrayList<PayGoodsList>();
		goodsList.add(goodsMessage);

		message.setGoodsList(FastJsonUtil.toJSONString(goodsList));

		message.setScanCodeId(message.getAuthCode());// 授权码
		message.setOutStlmCd(message.getBank());
		message.setMchntid(baseMsg.getMerID());
		message.setSignKey(baseMsg.getPrivateKeyPath());
		return agPaySpdbServiceClient.payment(message);
	}

	/**
	 * 
	 * 功能描述：条码支付订单查询接口报文构建
	 *
	 * @param obj
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2017年11月16日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object orderQuery(BaseMessage baseMsg) throws GopayException {

		QueryOrderBody message = new QueryOrderBody();
		BeanUtils.copyProperties(baseMsg, message);
		message.setVersion(agPaySpdbPayConfig.get("SPDB.AGPAY.VERSION"));
		message.setSignType(agPaySpdbPayConfig.get("SPDB.AGPAY.SIGNTYPE"));
		message.setCharset(agPaySpdbPayConfig.get("SPDB.AGPAY.CHARSET"));
		message.setTxndir("Q");// 交易方向，Q为请求，A为应答
		message.setBusicd(SpdbConstants.INQY);
		message.setInscd(agPaySpdbPayConfig.get("SPDB.AGPAY.INSCD"));// 机构号
		message.setMchntid(baseMsg.getMerID());
		message.setTerminalid(agPaySpdbPayConfig.get("SPDB.AGPAY.TERMINALID"));// 终端号

		message.setOrigOrderNum(message.getBankOrderId());// 交易订单号
		message.setMchntid(baseMsg.getMerID());
		message.setSignKey(baseMsg.getPrivateKeyPath());
		return agPaySpdbServiceClient.queryOrder(message);
	}

	/**
	 * 
	 * 功能描述：浦发退款报文构建
	 *
	 * @param obj
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2017年11月16日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object refund(BaseMessage baseMsg) throws GopayException {

		RefundBody message = new RefundBody();
		BeanUtils.copyProperties(baseMsg, message);
		
		message.setVersion(agPaySpdbPayConfig.get("SPDB.AGPAY.VERSION"));
		message.setSignType(agPaySpdbPayConfig.get("SPDB.AGPAY.SIGNTYPE"));
		message.setCharset(agPaySpdbPayConfig.get("SPDB.AGPAY.CHARSET"));
		message.setTxndir("Q");// 交易方向，Q为请求，A为应答
		message.setBusicd(SpdbConstants.REFD);
		message.setInscd(agPaySpdbPayConfig.get("SPDB.AGPAY.INSCD"));// 机构号
		message.setMchntid(baseMsg.getMerID());
		message.setTerminalid(agPaySpdbPayConfig.get("SPDB.AGPAY.TERMINALID"));// 终端号

		message.setOrderNum(baseMsg.getBankOrderId());
		message.setOrigOrderNum(baseMsg.getTkOriBankOrderId());// 原交易订单号
		message.setTxamt(TransUtil.amountConvert2Fen(String.valueOf(baseMsg.getAmount()), 12, "0"));
		message.setMchntid(baseMsg.getMerID());
		message.setSignKey(baseMsg.getPrivateKeyPath());
		return agPaySpdbServiceClient.refund(message);
	}

	/**
	 * 
	 * 功能描述：浦发银行条码撤销接口
	 *
	 * @param obj
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2017年11月16日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object reverse(BaseMessage baseMsg) throws GopayException {

		ReverseBody message = new ReverseBody();
		BeanUtils.copyProperties(baseMsg, message);
		
		message.setVersion(agPaySpdbPayConfig.get("SPDB.AGPAY.VERSION"));
		message.setSignType(agPaySpdbPayConfig.get("SPDB.AGPAY.SIGNTYPE"));
		message.setCharset(agPaySpdbPayConfig.get("SPDB.AGPAY.CHARSET"));
		message.setTxndir("Q");// 交易方向，Q为请求，A为应答
		message.setBusicd(SpdbConstants.CANC);
		message.setInscd(agPaySpdbPayConfig.get("SPDB.AGPAY.INSCD"));// 机构号
		message.setMchntid(baseMsg.getMerID());
		message.setTerminalid(agPaySpdbPayConfig.get("SPDB.AGPAY.TERMINALID"));// 终端号

		message.setOrderNum(baseMsg.getBankOrderId());
		message.setOrigOrderNum(baseMsg.getCzOriBankOrderId());// 原交易订单号
		message.setMchntid(baseMsg.getMerID());
		message.setSignKey(baseMsg.getPrivateKeyPath());
		return agPaySpdbExtServiceClient.reverse(message);

	}
	/**
	 * 
	 * 功能描述：浦发银行验签
	 *
	 * @param msg
	 * @return
	 * @throws GopayException
	 * 
	 * @author 宣广海
	 *
	 * @since 2017年11月20日
	 *
	 * @update:[变更日期YYYY-MM-DD][更改人姓名][变更描述]
	 */
	public Object verify(BaseMessage msg) throws GopayException {
		PayNotify verifyMsg = (PayNotify) msg;
		verifyMsg.setSignKey(msg.getPrivateKeyPath());
		verifyMsg.setSign(msg.getPlainMessage());
		return agPaySpdbServiceClient.verify(msg);
	}
	private static String modifyOrderId(String orderId) {
		return "SP" + orderId.substring(2);
	}
}
